<?php
/*
	我的下线
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	if(!$this->setting['level']){
		exi('未开启分销！','error','jy_merch/merch/');
	}
	$return['setting']=$this->plugin_setting('page_setting');
	$levels=array();
	$agent_ids=$_SESSION['uid'];
	$allagentnum=0;
	for($i=0;$i<$this->setting['level'];$i++){
		$level=$i+1;
		$levels[$level]['key']=$level;
		$levels[$level]['title']=$this->setting['levelname'.$level]?:$level.'级';
		if(!$agent_ids){
			$levels[$level]['num']=0;
			continue;
		}
		$agent_ids=$this->getall('member',array('agent_id'=>$agent_ids),array('mid'),'mid');
		if(!$agent_ids){
			$levels[$level]['num']=0;
		}else{
			$agent_ids=array_keys($agent_ids);
			$levels[$level]['num']=count($agent_ids);
			$allagentnum+=$levels[$level]['num'];
		}
	}
	$return['allagentnum']=$allagentnum;
	$return['levels']=$levels;
	$return['member']=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	$return['params']=array('page'=>1,'level'=>1);
	$return['psize']=20;
	$return['list']=array();
	set_html('title',$return['setting']['tmydown']?:'我的下线');
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	$level=intval($_RQ['level']);
	if(!$level){
		$level=1;
	}
	$agent_ids=$_SESSION['uid'];
	for($i=1;$i<$level;$i++){
		if(!$agent_ids){
			break;
		}
		$agent_ids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'agent_id'=>$agent_ids),array('mid'),'mid');
		if(!$agent_ids){
			break;
		}else{
			$agent_ids=array_keys($agent_ids);
		}
	}
	if(!$agent_ids){
		exi('none');
	}
	$params['agent_id']=$agent_ids;
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'mid desc',
	);
	$list=$this->get_list('member',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		if(!$item['member']['id']){
			pdo_delete('jy_mfee_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));	
		}
		$item['agenttime']=date('Y-m-d H:i:s',$item['agenttime']);
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['member_num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'agent_id'=>$item['mid']),array('mid'),'mid');
		$item['leveltitle']=$this->getcolumn('level',array('id'=>$item['level_id']),'title');
		$item['leveltitle']=$item['leveltitle']?:'默认等级';
	}
	exi($list);
}
?>